package com.edu.zju.scs.panacea.mapper.resource;

import com.edu.zju.scs.panacea.entity.query.LocQuery;
import com.edu.zju.scs.panacea.entity.resource.Loc;
import com.edu.zju.scs.panacea.entity.vo.LocVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-20 13:37
 */
@Repository
public interface LocMapper {
    @Select("SELECT * FROM res_loc WHERE id = #{id}")
    @Results(id="locMap", value={
            @Result(id=true,property="id",column="id"),
            @Result(property="devId",column="dev_id"),
            @Result(property="height",column="height"),
            @Result(property="width",column="width"),
            @Result(property="name",column="name")})
    Loc selectLoc(Long id);

    @Select("SELECT L.id, L.dev_id, L.height, L.width, L.name FROM res_loc as L join res_dev as D WHERE L.dev_id = D.id and D.site_id = #{siteId}")
    @ResultMap(value = "locMap")
    List<Loc> selectLocBySiteId(Long siteId);

    @Select("SELECT * FROM res_loc")
    @ResultMap(value = "locMap")
    List<Loc> selectAllLoc();

    @Delete("DELETE FROM res_loc where id = #{id}")
    long deleteLoc(Long id);

    @Insert("INSERT INTO res_loc(dev_id, name, height, width) VALUES (#{devId}, #{name}, #{height}, #{width})")
    @Options(useGeneratedKeys=true, keyColumn="id")
    long insertLoc(Loc loc);

    @Update({ "update res_loc set dev_id = #{devId}, name = #{name}, height = #{height}, width = #{width} where id = #{id}" })
    long updateLoc(Loc loc);

    @Delete("DELETE FROM res_loc where dev_id = #{devId}")
    long deleteLocByDev(Long devId);


    @Select("<script> " +
            "SELECT L.id, L.name, L.dev_id, L.height, L.width, P.province, P.city, P.district from res_loc as L join res_position as P WHERE L.dev_id = P.dev_id "+
            " <if test='locQuery.id != null'> and L.id = #{locQuery.id} </if> "+
            " <if test='locQuery.name != null'> and L.name = #{locQuery.name} </if> "+
            " <if test='locQuery.province != null'> and P.province = #{locQuery.province} </if> "+
            " <if test='locQuery.city != null'> and P.city = #{locQuery.city} </if> "+
            " <if test='locQuery.district != null'> and P.district = #{locQuery.district} </if> "+
            " <if test='locQuery.siteId != null'> and L.dev_id in (select id from res_dev where site_id = #{locQuery.siteId}) </if> "+
            " <if test='locQuery.templateType != null'> and L.id in (select TL.loc_id from res_template_rel_loc as TL join res_template as TMP where TL.template_id = TMP.id and TMP.type = #{locQuery.templateType}) </if> "+
            " </script>")
    @Results(value={
            @Result(id=true,property="id",column="id"),
            @Result(property="devId",column="dev_id"),
            @Result(property="height",column="height"),
            @Result(property="width",column="width"),
            @Result(property="name",column="name"),
            @Result(property="province",column="province"),
            @Result(property="city",column="city"),
            @Result(property="district",column="district")})
    List<LocVO> selectSpecificLocs(@Param("locQuery") LocQuery locQuery);
}
